﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASCalpuff.FileLayers
{

    /// <summary>
    /// 通用网格信息
    /// </summary>
    public class MG2:Group
    {
        //控制用的变量

        /// <summary>
        /// 是否启用通用网格,这样的话，需要通用网格给出具体文字
        /// </summary>
        bool m_bCommonGrid = true;

        //地图投影代码，UTM,TTM,LCC,PS,EM,LAZA
        private string m_PMAP = "UTM";
        //地图投影的误差-东西方向,km,仅且在PMAP= TTM, LCC, or LAZA时使用
        private double m_FEAST = 0.0;
        //地图投影的误差-南北方向,km,仅且在PMAP= TTM, LCC, or LAZA时使用
        private double m_FNORTH = 0.0;
        //UTM区号，仅且在PMAP= UTM时使用
        private int m_IUTMZN = 50;
        //UTM的南北半球标示，N 北半球，S 南半球，仅且在PMAP= UTM时使用
        private string m_UTMHEM = "N";
        //投影的起始点经纬度，纬度，小数表示
        private string m_RLAT0 = "10N";

        //public string RLAT0
        //{
        //    get { return m_RLAT0; }
        //    set { m_RLAT0 = value; }
        //}
        //投影的起始点经纬度，经度，小数表示
        private string m_RLON0 = "110E";

        //public string RLON0
        //{
        //    get { return m_RLON0; }
        //    set { m_RLON0 = value; }
        //}
        //两条标准纬线，纬度，小数表示
        private string m_XLAT1 = "25N";
        //两条标准纬线，经度，小数表示
        private string m_XLAT2 = "40N";
        //基准面代码
        private string m_DATUM = "WGS-84";
        //水平网格设置，X方向网格数
        private int m_NX = 30;
        //水平网格设置，Y方向网格数
        private int m_NY = 30;
        //水平网格设置，网格间距，单位km
        private double m_DGRIDKM = 1.0;
        //水平网格设置，X方向起始坐标，KM
        private double m_XORIGKM = 491.143;
        //水平网格设置，Y方向起始坐标，KM
        private double m_YORIGKM = 4050.045;
        //垂向网格设置，垂向网格层数
        private int m_NZ = 8;
        //垂向网格设置， 每层高度
        private List<double> m_ZFACE = new List<double>();



        public string GetStrMapGrid()
        {
            string Str = "-------------------------------------------------------------------------------\r\nINPUT GROUP: 2 -- Map Projection and Grid control parameters\r\n-------------------------------------------------------------------------------\r\n";
            string mEnd = "  !";
            Str = Str + "! PMAP = " + this.PMAP + mEnd + "\r\n";
            switch (this.PMAP)
            {
                case "UTM":
                    Str = Str + "! IUTMZN =  " + this.IUTMZN.ToString() + mEnd + "\r\n! UTMHEM = " + this.UTMHEM + mEnd + "\r\n";
                    break;

                case "LCC":
                    Str = Str + "! FEAST  = " + this.FEAST.ToString() + mEnd + "\r\n! FNORTH = " + this.FNORTH.ToString() + mEnd + "\r\n! IUTMZN =  " + this.IUTMZN.ToString() + mEnd + "\r\n! UTMHEM = " + this.UTMHEM + mEnd + "\r\n! RLAT0 =  " + this.RLAT0 + mEnd + "\r\n! RLON0 =  " + this.RLON0 + mEnd + "\r\n! XLAT1 =  " + this.XLAT1 + mEnd + "\r\n! XLAT2 =  " + this.XLAT2 + mEnd + "\r\n";
                    break;

                case "TTM":
                    Str = Str + "! FEAST  = " + this.FEAST.ToString() + mEnd + "\r\n! FNORTH = " + this.FNORTH.ToString() + mEnd + "\r\n! IUTMZN =  " + this.IUTMZN.ToString() + mEnd + "\r\n! UTMHEM = " + this.UTMHEM + mEnd + "\r\n! RLAT0 =  " + this.RLAT0 + mEnd + "\r\n! RLON0 =  " + this.RLON0 + mEnd + "\r\n";
                    break;

                case "LAZA":
                    Str = Str + "! FEAST  = " + this.FEAST.ToString() + mEnd + "\r\n! FNORTH = " + this.FNORTH.ToString() + mEnd + "\r\n! IUTMZN =  " + this.IUTMZN.ToString() + mEnd + "\r\n! UTMHEM = " + this.UTMHEM + mEnd + "\r\n! RLAT0 =  " + this.RLAT0 + mEnd + "\r\n! RLON0 =  " + this.RLON0 + mEnd + "\r\n";
                    break;

                case "PS":
                    Str = Str + "! IUTMZN =  " + this.IUTMZN.ToString() + mEnd + "\r\n! UTMHEM = " + this.UTMHEM + mEnd + "\r\n! RLAT0 =  " + this.RLAT0 + mEnd + "\r\n! RLON0 =  " + this.RLON0 + mEnd + "\r\n! XLAT1 =  " + this.XLAT1 + mEnd + "\r\n";
                    break;

                case "EM":
                    Str = Str + "! IUTMZN =  " + this.IUTMZN.ToString() + mEnd + "\r\n! UTMHEM = " + this.UTMHEM + mEnd + "\r\n! RLAT0 =  " + this.RLAT0 + mEnd + "\r\n! RLON0 =  " + this.RLON0 + mEnd + "\r\n";
                    break;
            }
            Str = Str + "! DATUM = " + this.DATUM + mEnd + "\r\n! NX =   " + this.NX.ToString() + mEnd + "\r\n! NY =   " + this.NY.ToString() + mEnd + "\r\n! DGRIDKM = " + this.DGRIDKM.ToString() + mEnd + "\r\n! XORIGKM = " + this.XORIGKM.ToString() + mEnd + "\r\n! YORIGKM = " + this.YORIGKM.ToString() + mEnd + "\r\n! NZ =  " + this.NZ.ToString() + mEnd + "\r\n";
            string mZface = "! ZFACE =";


            int VBst_i4sL3 = this.ZFACE.Count - 1;
            for (int i = 0; i <= VBst_i4sL3; i++)
            {
                mZface = mZface + " " + this.ZFACE[i].ToString() + ",";
            }
            return ((Str + mZface.Remove(mZface.Length - 1, 1) + mEnd + "\r\n") + "!END!\r\n");
        }


        #region 属性对应

        // Properties
        public string DATUM
        {
            get
            {
                return this.m_DATUM;
            }
            set
            {
                this.m_DATUM = value;
            }
        }

        public double DGRIDKM
        {
            get
            {
                return this.m_DGRIDKM;
            }
            set
            {
                this.m_DGRIDKM = value;
            }
        }

        public double FEAST
        {
            get
            {
                return this.m_FEAST;
            }
            set
            {
                this.m_FEAST = value;
            }
        }

        public double FNORTH
        {
            get
            {
                return this.m_FNORTH;
            }
            set
            {
                this.m_FNORTH = value;
            }
        }

        public int IUTMZN
        {
            get
            {
                return this.m_IUTMZN;
            }
            set
            {
                this.m_IUTMZN = value;
            }
        }

        public int NX
        {
            get
            {
                return this.m_NX;
            }
            set
            {
                this.m_NX = value;
            }
        }

        public int NY
        {
            get
            {
                return this.m_NY;
            }
            set
            {
                this.m_NY = value;
            }
        }

        public int NZ
        {
            get
            {
                return this.m_NZ;
            }
            set
            {
                this.m_NZ = value;
            }
        }

        public string PMAP
        {
            get
            {
                return this.m_PMAP;
            }
            set
            {
                this.m_PMAP = value;
            }
        }

        public string RLAT0
        {
            get
            {
                return this.m_RLAT0;
            }
            set
            {
                this.m_RLAT0 = value;
            }
        }

        public string RLON0
        {
            get
            {
                return this.m_RLON0;
            }
            set
            {
                this.m_RLON0 = value;
            }
        }

        public string UTMHEM
        {
            get
            {
                return this.m_UTMHEM;
            }
            set
            {
                this.m_UTMHEM = value;
            }
        }

        public string XLAT1
        {
            get
            {
                return this.m_XLAT1;
            }
            set
            {
                this.m_XLAT1 = value;
            }
        }

        public string XLAT2
        {
            get
            {
                return this.m_XLAT2;
            }
            set
            {
                this.m_XLAT2 = value;
            }
        }

        public double XORIGKM
        {
            get
            {
                return this.m_XORIGKM;
            }
            set
            {
                this.m_XORIGKM = value;
            }
        }

        public double YORIGKM
        {
            get
            {
                return this.m_YORIGKM;
            }
            set
            {
                this.m_YORIGKM = value;
            }
        }

        public List<double> ZFACE
        {
            get
            {
                return this.m_ZFACE;
            }
            set
            {
                this.m_ZFACE = value;
            }
        }


        #endregion


        public override string WriteToGroup()
        {

            //return "";
            //return base.WriteToGroup();
            string Subgroup_a = "", TempStr;

            TempStr = "INPUT GROUP: 2 -- Map Projection and Grid control parameters" + "\r\n";
            Subgroup_a += TempStr;




            if (PMAP != "")
                TempStr = StrWriter.GetKeyValStr("PMAP", PMAP);
            else
                TempStr = StrWriter.GetNullKeyValStr("PMAP");
            Subgroup_a += TempStr;


            TempStr = "! FEAST  =  " + FEAST.ToString() + " !" + "\r\n";
            Subgroup_a += TempStr;

            TempStr = "! FNORTH  =  " + FNORTH.ToString() + " !" + "\r\n";
            Subgroup_a += TempStr;

            TempStr = "! IUTMZN  =  " + IUTMZN.ToString() + " !" + "\r\n";
            Subgroup_a += TempStr;

            if (UTMHEM != "")
                TempStr = StrWriter.GetKeyValStr("UTMHEM", UTMHEM);
            else
                TempStr = StrWriter.GetNullKeyValStr("UTMHEM");
            Subgroup_a += TempStr;

            if (RLAT0 != "")
                TempStr = StrWriter.GetKeyValStr("RLAT0", RLAT0);
            else
                TempStr = StrWriter.GetNullKeyValStr("RLAT0");
            Subgroup_a += TempStr;

            if (RLON0 != "")
                TempStr = StrWriter.GetKeyValStr("RLON0", RLON0);
            else
                TempStr = StrWriter.GetNullKeyValStr("RLON0");
            Subgroup_a += TempStr;

            if (XLAT1 != "")
                TempStr = StrWriter.GetKeyValStr("XLAT1", XLAT1);
            else
                TempStr = StrWriter.GetNullKeyValStr("XLAT1");
            Subgroup_a += TempStr;

            if (XLAT2 != "")
                TempStr = StrWriter.GetKeyValStr("XLAT2", XLAT2);
            else
                TempStr = StrWriter.GetNullKeyValStr("XLAT2");
            Subgroup_a += TempStr;

            if (DATUM != "")
                TempStr = StrWriter.GetKeyValStr("DATUM", DATUM);
            else
                TempStr = StrWriter.GetNullKeyValStr("DATUM");
            Subgroup_a += TempStr;

            TempStr = "! NX  =  " + NX.ToString() + " !" + "\r\n";
            Subgroup_a += TempStr;

            TempStr = "! NY  =  " + NY.ToString() + " !" + "\r\n";
            Subgroup_a += TempStr;

            TempStr = "! DGRIDKM  =  " + DGRIDKM.ToString() + " !" + "\r\n";
            Subgroup_a += TempStr;

            TempStr = "! XORIGKM  =  " + XORIGKM.ToString() + " !" + "\r\n";
            Subgroup_a += TempStr;

            TempStr = "! YORIGKM  =  " + YORIGKM.ToString() + " !" + "\r\n";
            Subgroup_a += TempStr;

            TempStr = "! NZ  =  " + NZ.ToString() + " !" + "\r\n";
            Subgroup_a += TempStr;


            TempStr = "! ZFACE  =  ";
            for (int i = 0; i < ZFACE.Count; i++)
            {
                if (i == ZFACE.Count - 1)
                {
                    TempStr += ZFACE[i].ToString();
                }
                else
                {
                    TempStr += ZFACE[i].ToString() + ",   ";
                }


            }
            TempStr += "   ! " + "\r\n" + "!END!" + "\r\n";
            Subgroup_a += TempStr;

            return Subgroup_a;

        }
        public override void ReadFromGroup(Dictionary<string, string> dic)
        {
            foreach (KeyValuePair<string, string> m_dic in dic)
            {
                if (m_dic.Key == "PMAP")
                {
                    this.PMAP = m_dic.Value;
                }
                if (m_dic.Key == "FEAST")
                {
                    this.FEAST = Convert.ToDouble(m_dic.Value);
                }
                if (m_dic.Key == "FNORTH")
                {
                    this.FNORTH = Convert.ToDouble(m_dic.Value);
                }
                if (m_dic.Key == "IUTMZN")
                {
                    this.IUTMZN = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "UTMHEM")
                {
                    this.UTMHEM = m_dic.Value;
                }
                if (m_dic.Key == "RLAT0")
                {
                    this.RLAT0 = m_dic.Value;
                }
                if (m_dic.Key == "RLON0")
                {
                    this.RLON0 = m_dic.Value;
                }
                if (m_dic.Key == "XLAT1")
                {
                    this.XLAT1 = m_dic.Value;
                }

                if (m_dic.Key == "XLAT2")
                {
                    this.XLAT2 = m_dic.Value;
                }
                if (m_dic.Key == "DATUM")
                {
                    this.DATUM = m_dic.Value;
                }
                if (m_dic.Key == "NX")
                {
                    this.NX = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "NY")
                {
                    this.NY = Convert.ToInt32(m_dic.Value);
                }
                //b
                if (m_dic.Key == "DGRIDKM")
                {
                    this.DGRIDKM = Convert.ToDouble(m_dic.Value);
                }
                if (m_dic.Key == "XORIGKM")
                {
                    this.XORIGKM = Convert.ToDouble(m_dic.Value);
                }
                if (m_dic.Key == "YORIGKM")
                {
                    this.YORIGKM = Convert.ToDouble(m_dic.Value);
                }
                if (m_dic.Key == "NZ")
                {
                    this.NZ = Convert.ToInt32(m_dic.Value);
                }
                //f
                if (m_dic.Key == "ZFACE")
                {
                    this.ZFACE = StrWriter.ReturnDouList(m_dic.Value);
                }

            }
        }
        //小齐加
        /// <summary>
        /// 得到字符串中的数值
        /// </summary>
        /// <param name="Val"></param>
        /// <returns></returns>
        public double GetValue(string Val)
        {
            if (Val != null)
            {
                if (Val.Length > 1)
                {
                    double A = Convert.ToDouble(Val.Remove(Val.Length - 1, 1));
                    return A;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }

        }
        /// <summary>
        /// 得到字符串中最后一个字符
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public string GetString(string Str)
        {
            if (Str != null)
            {
                return Str.Remove(0, Str.Length - 1);
            }
            else
            {
                return "";
            }

        }


    }
}
